package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.hfb.Amount1Helper;
import com.atguigu.srb.core.hfb.Amount2Helper;
import com.atguigu.srb.core.hfb.Amount3Helper;
import com.atguigu.srb.core.hfb.Amount4Helper;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.pojo.entity.LendItemReturn;
import com.atguigu.srb.core.mapper.LendItemReturnMapper;
import com.atguigu.srb.core.pojo.entity.LendReturn;
import com.atguigu.srb.core.service.LendItemReturnService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 标的出借回款记录表 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2023-05-26
 */
@Service
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {

    public List<LendItemReturn> saveLendItemReturns(Lend lend, BigDecimal monthServiceRate, List<LendItem> lendItems, List<LendReturn> lendReturns) {
        Map<Integer, Long> lendReturnIdMap = lendReturns.stream()
                .collect(Collectors.toMap(LendReturn::getCurrentPeriod, LendReturn::getId));
        //7、生成投资人的回款计划
        // 每个投资记录 都生成还款期数个回款计划
        List<LendItemReturn> lendItemReturns = new ArrayList<>();//回款计划集合
        lendItems.forEach(lendItem -> {
            Map<Integer, BigDecimal> perMonthInterest2 = null;
            Map<Integer, BigDecimal> perMonthPrincipal2 = null;
            switch (lend.getReturnMethod()){
                case 1:
                    perMonthInterest2 = Amount1Helper.getPerMonthInterest(lendItem.getInvestAmount()
                            ,lendItem.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    perMonthPrincipal2 = Amount1Helper.getPerMonthPrincipal(lendItem.getInvestAmount()
                            , lendItem.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    break;
                case 2:
                    perMonthInterest2 = Amount2Helper.getPerMonthInterest(lendItem.getInvestAmount()
                            , lend.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    perMonthPrincipal2 = Amount2Helper.getPerMonthPrincipal(lendItem.getInvestAmount()
                            , lend.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    break;
                case 3:
                    perMonthInterest2 = Amount3Helper.getPerMonthInterest(lendItem.getInvestAmount()
                            , lend.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    perMonthPrincipal2 = Amount3Helper.getPerMonthPrincipal(lendItem.getInvestAmount()
                            , lend.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    break;
                default:
                    perMonthInterest2 = Amount4Helper.getPerMonthInterest(lendItem.getInvestAmount()
                            , lend.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    perMonthPrincipal2 = Amount4Helper.getPerMonthPrincipal(lendItem.getInvestAmount()
                            , lend.getLendYearRate().divide(new BigDecimal(100)), lend.getPeriod());
                    break;
            }


            //回款计划：lend_item_return
            //一个标的 多个投资记录 ， 一个还款计划对应所有投资记录的当前期的回款计划  一个投资记录生成一组回款计划
            for (Integer i = 1; i <= lend.getPeriod(); i++) {
                //遍历一次为lendItem生成一期回款计划
                LendItemReturn lendItemReturn = new LendItemReturn();
                //还款计划id： 获取第i期的还款计划的id
                lendItemReturn.setLendReturnId(lendReturnIdMap.get(i));
                // 投资记录id
                lendItemReturn.setLendItemId(lendItem.getId());
                // 标的id
                lendItemReturn.setLendId(lendItem.getLendId());
                // 投资人会员id
                lendItemReturn.setInvestUserId(lendItem.getInvestUserId());
                // 投资记录金额
                lendItemReturn.setInvestAmount(lendItem.getInvestAmount());
                // 当前投资记录的回款期数
                lendItemReturn.setCurrentPeriod(i);
                // 年化率
                lendItemReturn.setLendYearRate(lendItem.getLendYearRate());
                // 还款方式
                lendItemReturn.setReturnMethod(lend.getReturnMethod());

                if(lend.getReturnMethod().equals(4)){
                    if(i.equals(lend.getPeriod())){
                        lendItemReturn.setPrincipal(perMonthPrincipal2.get(1));
                        lendItemReturn.setInterest(perMonthInterest2.get(1));
                    }else{
                        lendItemReturn.setPrincipal(new BigDecimal(0));
                        lendItemReturn.setInterest(new BigDecimal(0));
                    }
                }else{
                    lendItemReturn.setPrincipal(perMonthPrincipal2.get(i));
                    lendItemReturn.setInterest(perMonthInterest2.get(i));
                }
                //本息
                lendItemReturn.setTotal(lendItemReturn.getInterest().add(lendItemReturn.getPrincipal()));
                //手续费： 只扣除收益的手续费
                BigDecimal fee = monthServiceRate.multiply(lendItemReturn.getInterest());
                lendItemReturn.setFee(fee);
                //回款日
                lendItemReturn.setReturnDate(new DateTime(lend.getLendStartDate()).plusMonths(i).toDate());
                lendItemReturn.setOverdue(false);
                lendItemReturn.setOverdueTotal(new BigDecimal(0));
                //回款记录状态：默认未回款
                lendItemReturn.setStatus(0);
                lendItemReturns.add(lendItemReturn);
            }
        });
        this.saveBatch(lendItemReturns);
        return lendItemReturns;
    }

    @Override
    public List<LendItemReturn> listByLendIdAndUserId(String lendId, String token) {
        Long userId = JwtUtils.getUserId(token);

        return   this.list(Wrappers.lambdaQuery(LendItemReturn.class)
                .eq(LendItemReturn::getInvestUserId,userId)
                .eq(LendItemReturn::getLendId , lendId)
                .orderByAsc(LendItemReturn::getCurrentPeriod));
    }

}
